package com.jojo.jojomall.product.app;

import java.util.Arrays;
import java.util.Map;

import com.jojo.jojomall.product.service.CategoryService;
import com.jojo.jojomall.product.vo.AttrGroupRelationVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.jojo.jojomall.product.entity.AttrGroupEntity;
import com.jojo.jojomall.product.service.AttrGroupService;
import com.jojo.common.utils.PageUtils;
import com.jojo.common.utils.R;



/**
 * 属性分组
 *
 * @author jojoda
 * @email sunlightcs@gmail.com
 * @date 2022-05-01 20:46:22
 */
@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    private CategoryService categoryService;

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    public R list(@RequestParam Map<String, Object> params,@PathVariable("catelogId") Long catelogId){
        PageUtils page = attrGroupService.queryPage(params,catelogId);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public R info(@PathVariable("attrGroupId") Long attrGroupId){
		AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
       Long categlogId= attrGroup.getAttrGroupId();
        Long [] path=  categoryService.findPathById(categlogId);
       attrGroup.setCatelogPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody AttrGroupEntity attrGroup){
		attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] attrGroupIds){
		attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }
 @RequestMapping("/relation/delete")
    public R deleteRelation(@RequestBody AttrGroupRelationVo[] vos){
//        删除属性分组就行不是删除属性；
     //一，因为提交的参数是一个对象的集合，所以首先创建一个VO来接守
//     创建一个方法去实现
     attrGroupService.deleteRelation(vos);

        return R.ok();
    }
//    获取属性分组没有关联的其他属性
// 链接/product/attrgroup/{attrgroupId}/noattr/relation
    @RequestMapping("/{attrgroupId}/noattr/relation")
    public R noattrRealeation(@RequestParam Map<String, Object> params,
                              @PathVariable("attrgroupId") Long attrgroupId){
     PageUtils page  =attrGroupService.noattrRelation(params,attrgroupId);
     return R.ok().put("page",page);
    }
}
